package main

import "fmt"

func lengthOfLongestSubstring(s string) int {
	//用于存储字符及其最后出现的位置
	charIndex := make(map[rune]int)
	//left 是滑动窗口的左边界，maxLength 用于记录最长子串的长度
	left, maxLength := 0, 0
	for right, char := range s {
		index, exists := charIndex[char]
		if exists && index >= left {
			left = index + 1
		}
		charIndex[char] = right
		maxLength = max(maxLength, right-left+1)
	}
	return maxLength
}

func main() {
	s := "abcabcbb"
	fmt.Println(lengthOfLongestSubstring(s)) // 输出: 3
}
